package com.learn.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect.<br>
 * If the two linked lists have no intersection at all, return null.<br>
 *
 * The test cases are generated such that there are no cycles anywhere in the entire linked structure.<br>
 *
 * Note that the linked lists must retain their original structure after the function returns.<br>
 * </p>
 *
 * @author guapi
 * @date 2023-09-27
 */
public class num_160 {
    public static void main(String[] args) {

    }

    /**
     * 找到相交的节点
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (null == headA || null == headB) {
            return null;
        }
        ListNode tmpA = headA;
        ListNode tmpB = headB;

        while (tmpA != tmpB) {
            tmpA = tmpA == null ? headB : tmpA.next;
            tmpB = tmpB == null ? headA : tmpB.next;
        }
        return tmpA;
    }

    public class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
